package se.webbzon.boltzmann.game.object.portal;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import se.webbzon.oschi01.worldeditor.WorldStaticObjectDeflation;

public abstract class AbstractPortalDeflation extends WorldStaticObjectDeflation {
	
	private String worldAreaName;
	
	private byte portalId;

	/*============================================================
	Constructors
	============================================================*/
	
	public AbstractPortalDeflation(int deflationProperties) {
		super(deflationProperties,null);
	}
	
	public AbstractPortalDeflation(int deflationProperties, AbstractPortal portal) {
		super(deflationProperties,null,portal);
		worldAreaName = portal.getWorldAreaName();
		portalId = portal.getPortalId();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	public String getWorldAreaName() {
		return worldAreaName;
	}
	
	public byte getPortalId() {
		return portalId;
	}
	
	@Override public ByteBuffer deflate(int allocate) {
		byte[] worldAreaNameData;
		try {
			worldAreaNameData  = worldAreaName.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			worldAreaNameData  = new byte[0];
		}
		ByteBuffer buff = super.deflate(allocate + worldAreaNameData.length + 3);
		buff.putShort((short) worldAreaNameData.length);
		buff.put(worldAreaNameData);
		buff.put(portalId);
		return buff;
	}
	
	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		byte[] worldAreaNameData = new byte[buff.getShort()];
		buff.get(worldAreaNameData, 0, worldAreaNameData.length);
		try {
			worldAreaName = new String(worldAreaNameData,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			worldAreaName = "";
		}
		portalId = buff.get();
	}

}
